package com.atguigu.lease.web.admin.controller.apartment;


import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.entity.FacilityInfo;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.service.FacilityInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Tag(name = "配套管理")
@RestController
@RequestMapping("/admin/facility")
public class FacilityController {

    @Autowired
    private FacilityInfoService facilityInfoService;



    /*
    springMVC中的controller方法可以接受如下几种类型的参数：
    1. 请求参数
       - request.getParameter("");
       - @RequestParam()
         http://localhost:8080/user?uid=u001&pwd=123
         public Result login(@RequestParam("uid") String uid , @RequestParam("pwd") String pwd ){}
         当queryString中的名称和controller中的方法参数名称一致时，@RequestParam注解可以省略
         public Result login(String uid , @RequestParam("pwd") String pwd ){}
    2. 请求体参数
       @RequestBody
    3. 路径参数
       @PathVariable
    4. 请求头参数
       @RequestHeader() /  @Cookie()
     */

    /*
    当前前端发送过来的是数字1或者2.而控制器中的方法参数是ItemType枚举类型。   1或者2   和   ItemType 是不能互相转换的
    String类型的 ： “18”     但是controller中的方法参数是：  Integer age   此时，springMVC中有自动的类型转换器可以将String转换成Integer
    解决方法：自定义类型转换器
     */

    @Operation(summary = "[根据类型]查询配套信息列表")
    @GetMapping("list")
    public Result<List<FacilityInfo>> listFacility(@RequestParam(required = false) ItemType type) {
        LambdaQueryWrapper<FacilityInfo> facilityInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //facilityInfoLambdaQueryWrapper.eq(FacilityInfo::getType,type);
        facilityInfoLambdaQueryWrapper.eq(type!=null , FacilityInfo::getType,type);
        List<FacilityInfo> facilityInfoList = facilityInfoService.list(facilityInfoLambdaQueryWrapper);
        return Result.ok(facilityInfoList);
    }

    @Operation(summary = "新增或修改配套信息")
    @PostMapping("saveOrUpdate")
    public Result saveOrUpdate(@RequestBody FacilityInfo facilityInfo) {
        facilityInfoService.saveOrUpdate(facilityInfo);
        return Result.ok();
    }

    @Operation(summary = "根据id删除配套信息")
    @DeleteMapping("deleteById")
    public Result removeFacilityById(@RequestParam Long id) {
        facilityInfoService.removeById(id);
        return Result.ok();
    }

}
